package ai.leinao.notify.email;

import ai.leinao.notify.core.enums.NotifyType;
import ai.leinao.notify.core.AbstractNotifier;
import ai.leinao.notify.core.NotifyProperties;
import ai.leinao.notify.core.constant.ConstantPool;
import ai.leinao.notify.core.template.TemplateRegistry;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * email 通知器
 *
 * @author zhong.wang
 * @date 2025/7/9
 */
@Getter
@Setter
public class EmailNotifier extends AbstractNotifier<EmailTemplate> {
    /**
     * 邮件发送者
     */
    private final JavaMailSender javaMailSender;

    /**
     * 邮件配置
     */
    private final EmailProperties emailProperties;

    /**
     * id
     */
    private final String notifyId;

    /**
     * 邮件发送者邮箱
     */
    private final String sender;

    /**
     * 邮件发送者
     */
    private final String username;



    /**
     * constructor
     * @param properties
     * @param templateRegistry
     */
    public EmailNotifier(NotifyProperties properties, TemplateRegistry templateRegistry) {
        this(properties.getId(),EmailProperties.of(properties), templateRegistry);
    }

    /**
     * constructor
     * @param properties
     * @param templateRegistry
     */
    public EmailNotifier(String notifyId,
                         EmailProperties properties,
                         TemplateRegistry templateRegistry) {
        super(templateRegistry);
        this.notifyId = notifyId;
        this.emailProperties = properties;
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(properties.getHost());
        mailSender.setPort(properties.getPort());
        mailSender.setUsername(properties.getUsername());
        mailSender.setPassword(properties.getPassword());
        mailSender.setJavaMailProperties(properties.convertProperties());
        this.sender = properties.getSender();
        this.username = properties.getUsername();
        this.javaMailSender = mailSender;
    }

    /**
     * 获取通知器类型
     *
     * @return 通知器类型
     * @see NotifyType
     */
    @Override
    public NotifyType getNotifyType() {
        return NotifyType.EMAIL;
    }

    /**
     * 获取通知器供应商
     *
     * @return
     */
    @Override
    public String getProvider() {
        return NotifyType.EMAIL.getType();
    }

    /**
     * 发送通知
     * @param template 模板
     * @param context 上下文
     */
    @Override
    public void send(EmailTemplate template, Map<String, Object> context) {
        template.renderVariable(context);
        try {
            MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, ConstantPool.UTF_8);
            if (StrUtil.isNotBlank(this.username)) {
                helper.setFrom(this.sender + ConstantPool.LT + this.username + ConstantPool.GT);
            } else {
                helper.setFrom(this.sender);
            }
            helper.setTo(template.getSendTo().split(ConstantPool.COMMA));
            helper.setSubject(template.getSubject());
            helper.setText(new String(template.getText().getBytes(), StandardCharsets.UTF_8), false);
            this.javaMailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
